package handlers

import (
	"brood-agent2/controller"
	"brood-agent2/utils"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

type KubeApiServerInstaller struct {
	PkgUrl         DownloadUrl   `json:"pkg_url"`
	ApplicationDir string        `json:"application_dir"`
	KubectlEnv     string        `json:"kubectl_env"`
	DependFiles    []DownloadUrl `json:"depend_files"`
}

type KubeSchedulerInstaller struct {
	PkgUrl         DownloadUrl   `json:"pkg_url"`
	ApplicationDir string        `json:"application_dir"`
	DependFiles    []DownloadUrl `json:"depend_files"`
}

type KubeControllerManagerInstaller struct {
	PkgUrl         DownloadUrl   `json:"pkg_url"`
	ApplicationDir string        `json:"application_dir"`
	DependFiles    []DownloadUrl `json:"depend_files"`
}

type KubeletInstaller struct {
	PkgUrl         DownloadUrl   `json:"pkg_url"`
	ApplicationDir string        `json:"application_dir"`
	DependFiles    []DownloadUrl `json:"depend_files"`
}

type KubeProxyInstaller struct {
	PkgUrl         DownloadUrl   `json:"pkg_url"`
	ApplicationDir string        `json:"application_dir"`
	DependFiles    []DownloadUrl `json:"depend_files"`
}

type BashCompletionInstaller struct {
	PkgUrl         DownloadUrl `json:"pkg_url"`
	ApplicationDir string      `json:"application_dir"`
}

func InstallKubeApiServerHandler(rw http.ResponseWriter, req *http.Request) {
	res := controller.JsonResponse{
		Code:    0,
		Data:    nil,
		Message: "success",
	}
	body, err := io.ReadAll(req.Body)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}
	var apiServerInstaller KubeApiServerInstaller
	err = json.Unmarshal(body, &apiServerInstaller)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}

	utils.Logger.Info(fmt.Sprintf("apiServerInstaller: %+v", apiServerInstaller))
	err = InstallKubeApiServerImpl(apiServerInstaller)
	if err != nil {
		res.Code = controller.CodeError
		res.Message = fmt.Sprintf("%s", err)
	}

	controller.RenderJsonResponse(rw, &res)
	return
}

func InstallKubeSchedulerHandler(rw http.ResponseWriter, req *http.Request) {
	res := controller.JsonResponse{
		Code:    0,
		Data:    nil,
		Message: "success",
	}
	body, err := io.ReadAll(req.Body)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}
	var schedulerInstaller KubeSchedulerInstaller
	err = json.Unmarshal(body, &schedulerInstaller)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}

	utils.Logger.Info(fmt.Sprintf("schedulerInstaller: %+v", schedulerInstaller))
	err = InstallKubeSchedulerImpl(schedulerInstaller)
	if err != nil {
		res.Code = controller.CodeError
		res.Message = fmt.Sprintf("%s", err)
	}

	controller.RenderJsonResponse(rw, &res)
	return
}

func InstallKubeControllerManagerHandler(rw http.ResponseWriter, req *http.Request) {
	res := controller.JsonResponse{
		Code:    0,
		Data:    nil,
		Message: "success",
	}
	body, err := io.ReadAll(req.Body)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}
	var controllerManagerInstaller KubeControllerManagerInstaller
	err = json.Unmarshal(body, &controllerManagerInstaller)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}

	utils.Logger.Info(fmt.Sprintf("controllerManagerInstaller: %+v", controllerManagerInstaller))
	err = InstallKubeControllerManagerImpl(controllerManagerInstaller)
	if err != nil {
		res.Code = controller.CodeError
		res.Message = fmt.Sprintf("%s", err)
	}

	controller.RenderJsonResponse(rw, &res)
	return
}

func InstallKubeletHandler(rw http.ResponseWriter, req *http.Request) {
	res := controller.JsonResponse{
		Code:    0,
		Data:    nil,
		Message: "success",
	}
	body, err := io.ReadAll(req.Body)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}
	var kubeletInstaller KubeletInstaller
	err = json.Unmarshal(body, &kubeletInstaller)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}

	utils.Logger.Info(fmt.Sprintf("kubeletInstaller: %+v", kubeletInstaller))
	err = InstallKubeletImpl(kubeletInstaller)
	if err != nil {
		res.Code = controller.CodeError
		res.Message = fmt.Sprintf("%s", err)
	}

	controller.RenderJsonResponse(rw, &res)
	return
}

func InstallKubeProxyHandler(rw http.ResponseWriter, req *http.Request) {
	res := controller.JsonResponse{
		Code:    0,
		Data:    nil,
		Message: "success",
	}
	body, err := io.ReadAll(req.Body)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}
	var kubeProxyInstaller KubeProxyInstaller
	err = json.Unmarshal(body, &kubeProxyInstaller)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}

	utils.Logger.Info(fmt.Sprintf("kubeProxyInstaller: %+v", kubeProxyInstaller))
	err = InstallKubeProxyImpl(kubeProxyInstaller)
	if err != nil {
		res.Code = controller.CodeError
		res.Message = fmt.Sprintf("%s", err)
	}

	controller.RenderJsonResponse(rw, &res)
	return
}

func InstallBashCompletionHandler(rw http.ResponseWriter, req *http.Request) {
	res := controller.JsonResponse{
		Code:    0,
		Data:    nil,
		Message: "success",
	}
	body, err := io.ReadAll(req.Body)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}
	var bashCompletionInstaller BashCompletionInstaller
	err = json.Unmarshal(body, &bashCompletionInstaller)
	if err != nil {
		controller.RenderJsonResponse(rw, &controller.JsonResponse{controller.CodeError, nil, fmt.Sprint(err)})
		return
	}

	utils.Logger.Info(fmt.Sprintf("bashCompletionInstaller: %+v", bashCompletionInstaller))
	err = InstallBashCompletion(bashCompletionInstaller)
	if err != nil {
		res.Code = controller.CodeError
		res.Message = fmt.Sprintf("%s", err)
	}

	controller.RenderJsonResponse(rw, &res)
	return
}
